﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pasys.PresentationCore;
using Pasys.PresentationCore.WinForms;
using UIShell.OSGi;

namespace DBExplorer
{
    public partial class ShellPanelLayoutView : UserControl
    {
        public IShellLayoutViewProxy LayoutViewProxy { get; private set; }

        public ShellPanelLayoutView()
        {
            InitializeComponent();

            LayoutViewProxy = new ShellLayoutViewProxy(this, Show)
            {
                MainMenuStrip = menuStrip1,
                MainStatusStrip = null,
                StatusLabel = null,
                ToolStripContainer = toolStripContainer1
            };

        }

        public void Show(IPersistViewProvider viewProvider)
        {
            Show(viewProvider.View, viewProvider.ViewInfo);
        }

        /// <summary>
        /// Shows SmartPart using the given SmartPartInfo
        /// </summary>
        /// <param name="smartPart">Smart part to show.</param>
        /// <param name="smartPartInfo"></param>
        private void Show(object smartPartKey, object smartPartInfo)
        {
            for (int i = panel1.Controls.Count - 1; i >= 0;i-- )
            {
                panel1.Controls[i].Dispose();
            }
            panel1.Controls.Clear();
            Control c = smartPartKey as Control;
            if (c == null)
            {
                return;
            }
            var f = c as Form;
            if (f != null)
            {
                f.TopLevel = false;
                f.FormBorderStyle = FormBorderStyle.None;
            }
            c.Dock = DockStyle.Fill;
            c.Visible = true;
            panel1.Controls.Add(c);

            //if (smartPartKey != null)
            //{
            //    MessageBox.Show(smartPartKey.ToString());
            //}
            
        }

        private void ShellPanelLayoutView_Load(object sender, EventArgs e)
        {
            var layoutViewProxy = BundleRuntime.Instance.GetFirstOrDefaultService<IShellLayoutViewProxy>();

            int count = layoutViewProxy.MainMenuStrip.Items.Count;
            for (int i = count - 1; i >= 0; i--)
            {
                ToolStripItem orgItem = (ToolStripMenuItem)layoutViewProxy.MainMenuStrip.Items[i];
                ToolStripMenuItem newItem = new ToolStripMenuItem(orgItem.Text);
                copyMenu(orgItem, newItem);
                this.menuStrip1.Items.Insert(0, newItem);
            }

        }

        private void copyMenu(ToolStripItem orgItem, ToolStripDropDownItem newItem)
        {
            var cmd = orgItem as ToolBarCommand;
            if (cmd != null)
            {
                newItem.DropDownItems.Add((ToolBarCommand)cmd.Clone());
                return;
            }
            else if (orgItem is ToolStripSeparator)
            {
                newItem.DropDownItems.Add(new ToolStripSeparator());
                return;
            }

            var dropDownItem = orgItem as ToolStripDropDownItem;
            if (dropDownItem.DropDownItems.Count > 0)
            {
                //var parentItem = newItem;
                foreach (ToolStripItem item in dropDownItem.DropDownItems)
                {
                    var subItem = item as ToolStripDropDownItem;
                    if (subItem != null && subItem.DropDownItems.Count > 0)
                    {
                        var parentItem = new ToolStripMenuItem(item.Text);
                        newItem.DropDownItems.Add(parentItem);
                        copyMenu(subItem, parentItem);
                    }
                    else
                    {
                        copyMenu(item, newItem);
                    }
                }
            }
        }

    }
}
